<?php
session_start();
include "class.Validator.php";

if($_POST)
	{
		if(isset($_POST['login']) && $_POST['login'] && isset($_POST['pwd']) && $_POST['pwd'] && isset($_POST['mail']) && $_POST['mail'])
		{
			$val = new Validator();
			if(!$val->validAlfanumeric($_POST['login']) || !$val->validSpace($_POST['login']))
			{
				$error_mess = 'Špatný login';	
			}
			if(!$val->validPasswd($_POST['pwd'],$_POST['login']))
			{
				$error_mess = 'Špatné heslo';
			}
			if(!$val->validEmail($_POST['mail']))
			{
				$error_mess = 'Špatný email';
			}
			
			if(!$error_mess)
			{		
				$connection = mysql_connect('localhost', 'panospet', 'meipei2c');
				if (!$connection) {
				    die('Could not connect: ' . mysql_error());
				}
				
				mysql_query("SET NAMES utf8");
				mysql_select_db("panospet");
				
				$query = "SELECT COUNT(*) FROM ukol7_users WHERE login = '".$_POST['login']."' LIMIT 1";
				$result = mysql_query($query);
				if (!$result) {
				    die('Chyba ve spusteni dotazu: ' . mysql_error());
				}	
				$existuje = mysql_fetch_assoc($result);
				
				if ($existuje && $existuje['COUNT(*)'] > 0)
				{
					$error_mess = 'Uživatel již existuje';					
				}
				else
				{
					$new_pwd = SHA1($_POST['pwd'].'sůl&ěščřžýáíé');
					$query = "INSERT INTO ukol7_users (login ,pwd, email) VALUES ('".$_POST['login']."','".$new_pwd."','".$_POST['mail']."')";
					$result = mysql_query($query);
					if (!$result) {
					    die('Chyba ve spusteni dotazu: ' . mysql_error());
					}
					
					$query = "SELECT * FROM ukol7_users WHERE login = '".$_POST['login']."'";
					$result = mysql_query($query);
					if (!$result) {
					    die('Chyba ve spusteni dotazu: ' . mysql_error());
					}	
					
					mysql_close($connection);
					
					$uzivatel = mysql_fetch_assoc($result);
					if($uzivatel)
					{
						$_SESSION['id'] = $uzivatel['id'];
						$_SESSION['login'] = $uzivatel['login'];
						$_SESSION['pwd'] = $uzivatel['pwd'];					
						$_SESSION['email'] = $uzivatel['email'];
						echo "<meta http-equiv='refresh' content='0;url=index.php?cmd=2'>";						
					}
					else
					{
						$error_mess = "Nepodařilo se načíst data uživatele.";	
					}
				}				
			}
		}
		else
		{
			$error_mess = 'Nebyla vyplněna všechna pole.';	
		}
	}
	if($error_mess)
	{
		echo "<p>".$error_mess."</p>";	
	}

?>

<form method="post">
    <table>
        <tr>
            <td><label for="login"> Login </label></td>
            <td><input type="text" name="login" id="login"></td>
        </tr>
        <tr>
            <td><label for="pwd"> Heslo </label></td>
            <td><input type="password" name="pwd" id="pwd"></td>
        </tr>
        <tr>
            <td><label for="mail"> Email </label></td>
            <td><input type="email" name="mail" id="mail"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Zaregistrovat"></td>
        </tr>
    </table>
</form>
